

$(document).ready(function() {
	keyDownToLogin();
});

//登录操作
function login(){
	var account =$.trim($("#accountIpt").val());
	var password = $("#passwordIpt").val();
	var isRemember = $("#rememberIpt").is(':checked');
	
	if(account==null || $.trim(account).length <= 0){
		showErrorTips('account','请输入登录名');
		return;
	}
	
	if(password==null || $.trim(password).length <= 0){
		showErrorTips('password','请输入密码');
		return;
	}
	
//	blockUI();
	
	var reqParams={
//        	username:account,
//        	password:password
        	account:account,
        	pwd:password
    };
	
	var headerParams ={};
	
	if(isRemember){
//		headerParams["remember-me"]=true;
		reqParams["remember-me"]=true;
	}
	
	
	$.ajax({
        url:ctx+"/signin",
        headers:headerParams,
        data:reqParams,
        type:"post",
        dataType:"json",
        success:function(result){
         	console.log("login result:",result);
         	if(result.code==0){
        		if(!!result.data.targetUrl){
        			window.location.href= result.data.targetUrl;
        		}else{
        			window.location.href= ctx+"/home";
        		}
        	}else{
//        		unBlockUI();
//        		showMsgDialog("登录失败:"+result.error);
//        		$("#loginBox").addClass("hasError");
        		showErrorTips(result.errorType,result.error)
        	}
        },
        error:function(){
        	console.info("login error");
        },
        complete:function(){
        	
        }
    });
}

//退出操作
function logout(){
	window.location.href=ctx+"/signout";
}

//确认键自动执行登录
function keyDownToLogin(){
	$("body").keydown(function() {
	    if (event.keyCode == "13") {//keyCode=13是回车键
	    		login();
	    }
	});
}


function showErrorTips(errorType,error){
	if(errorType=='password'){
		$("#pwdDiv").addClass("hasError");
		$("#pwdError").removeClass("none");
		$("#pwdError").text(error);
	}else{
		$("#accountDiv").addClass("hasError");
		$("#accountError").removeClass("none");
		$("#accountError").text(error);
	}
}

function clearErrorTips(type){
	if(type=='pwd'){
		$("#pwdDiv").removeClass("hasError");
		$("#pwdError").addClass("none");
		$("#pwdError").text('');
	}else{
		$("#accountDiv").removeClass("hasError");
		$("#accountError").addClass("none");
		$("#accountError").text('');
	}
}
